<?php

class Jtt
{
    private $union;
    public function __construct($union)
    {
        $this->union = $union;
    }

    public function curlRequestForPost($url, $post_data=[])
    {
        if (is_array($post_data)) {
            $post_data = http_build_query($post_data);
        }
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        // 20s 超时
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 20);
        curl_setopt($curl, CURLOPT_TIMEOUT, 20);

        //设置post方式提交
        curl_setopt($curl, CURLOPT_POST, 1);
        // post参数
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
        $list_stat = curl_exec($curl);
        // curl 出错
        if (curl_errno($curl)) {
            return [
                'success' => false,
                'msg'     => curl_error($curl)
            ];
        }
        curl_close($curl);
        return $list_stat;
    }



    /**
     * @param $data 数据转换
     */
    public function trans_data($data)
    {
        $new_data = [];
        foreach ($data as $key => $value) {
            $new_data[$key]["GoodsId"]       = "{$value['goods_id']}";
            $new_data[$key]["Title"]         = $value["goods_name"];
            $new_data[$key]["ShortTitle"]    = isset($value['short_name']) && !empty($value['short_name']) ? $value['short_name'] : $value['goods_name'];
            $new_data[$key]["ShopType"]      = '京东';
            $new_data[$key]["PicLogo"]       = $value['goods_img'];
            $new_data[$key]["CouponMoney"]   = $value['discount_price'];
            $new_data[$key]["Price"]         = $value['goods_price'];
            $new_data[$key]["EndPrice"]      = isset($value['final_price']) ? $value['final_price'] : $value['discount_price'];
            $new_data[$key]["MonthSale"]     = $value['inOrderCount30Days'];
            $new_data[$key]["Site"]          = "jd";
            $new_data[$key]["ItemFrom"]      = "jtt";
            $new_data[$key]['Commission']    = isset($value['final_price']) ? ($value['final_price'] * $value['commissionShare'] / 100) : ($value['discount_price'] * $value['commissionShare'] / 100);
            $new_data[$key]["Couponurl"]     = $value['discount_link'];
            $new_data[$key]["ShopName"]      = isset($value['shop_name']) && $value['shop_name'] ? $value['shop_name'] : '';
            $new_data[$key]["CouponTotal"]   = rand(100,1000);
            $new_data[$key]["CouponSurplus"] = rand(100,500);
            $new_data[$key]["IsAd"]          = 0;
        }
        return $new_data;
    }



    //京推推9.9包邮
    public function nine_ti_nine($params=[]){
        $url = "http://japi.jingtuitui.com/api/get_price_9_9";
        $postdata['appid'] = $this->union['jttAppId'];
        $postdata['appkey'] = $this->union['jttAPPKey'];
        $postdata['page'] = !isset($params['page']) && empty($params['page']) ? 1 : $params['page'];
        $postdata['num'] = !isset($params['page_size']) && empty($params['page_size']) ? 10 : $params['page_size'];
        $postdata['goods_new_type'] = !isset($params['goods_new_type']) && empty($params['goods_new_type']) ? 1 : $params['goods_new_type'];
        $result_str = $this->curlRequestForPost($url,$postdata);
        $result = json_decode($result_str,true);
        if($result['return'] == 0){
            $result_data = $this->trans_data($result['result']['data']);
            return ['code'=>true,'data'=>$result_data,'msg'=>'获取成功'];
        }else{
            return ['code'=>false,'msg'=>$result['result']];
        }
    }

    //京推推精选好货
    public function jxhh($params=[]){
        $url = "http://japi.jingtuitui.com/api/get_goods_list?eliteId=sift";
        $postdata['appid'] = $this->union['jttAppId'];
        $postdata['appkey'] = $this->union['jttAPPKey'];
        $postdata['v'] = 'v2';
        $postdata['pageIndex'] = !isset($params['page']) && empty($params['page']) ? 1 : $params['page'];
        $postdata['pageSize'] = !isset($params['page_size']) && empty($params['page_size']) ? 10 : $params['page_size'];
        $postdata['sortName'] = 'goodComments';
        if(!isset($param['order_by']) && empty($param['order_by'])){
            $postdata['sortName'] = 'goodComments';
        }
        $postdata['sort'] = 'desc';
        if(!isset($param['sort']) && empty($param['sort'])){
            $postdata['sort'] = 'desc';
        }
        $result_str = $this->curlRequestForPost($url,$postdata);
        $result = json_decode($result_str,true);
        if($result['return'] == 0){
            $result_data = $this->trans_data($result['result']['data']);
            return ['code'=>true,'data'=>$result_data,'msg'=>'获取成功'];
        }else{
            return ['code'=>false,'msg'=>$result['result']];
        }
    }



    /**
     * 京推推转链
     */
    public function get_goods_link($positionid,$gid,$coupon_url=''){
        $url = 'http://japi.jingtuitui.com/api/get_goods_link';
        $data['appid'] = $this->union['jttAppId'];
        $data['appkey'] = $this->union['jttAPPKey'];
        $data['unionid'] = $this->union['unionId'];
        $data['positionid'] = $positionid;
        $data['gid'] = $gid;
        if($coupon_url){
            $data['coupon_url'] = $coupon_url;
        }
        $responseResult = \Common\Common::request_post($url,$data);
        if(!$responseResult['status']){
            return ['status'=>false,'msg'=>$responseResult['msg']];
        }
        $result = $responseResult['data'];
        if($result['return'] == 0){
            $responseMsg['url']       = $result['result']['link'];
            $responseMsg['short_url'] = $result['result']['link'];
            $responseMsg['app_url'] = str_replace("https://union-click.jd.com/jdc", "openapp.jdmobile://virtual",$result['result']['link']);
            return ['status' => true, 'msg' => '获取成功', 'data' => $responseMsg];
        }else{
            return ['status' => false, 'msg' =>$result['result']];
        }
    }


    /**
     * 聚推客联盟统一活动转链接口 https://www.jutuike.com/doc/48
     * @param $user_id
     * @param $activity_id
     * @return array
     */
    public function turnLink($user_id,$activity_id){
        $url = "http://api.jutuike.com/union/act";
        $h_params['apikey'] = "qkKAlP8Avna1soTpJvahYRbZRgIWbTkT";
        $h_params['sid'] = $user_id;
        $h_params['act_id'] = $activity_id;
        $responseResult = $this->curlRequestForPost($url,$h_params);
        return ['status'=>true,'data'=>$responseResult];
    }

}
